package com.example.springbootconfig.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;

@Configuration
public class YamlDbConfig {

    @Autowired
    private Environment env;
    @Bean
    @Primary
    public DataSource getDataSource1() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(env.getProperty("jdbc.user"));
        dataSource.setPassword(env.getProperty("jdbc.password"));
        dataSource.setDriverClassName(env.getProperty("jdbc.driver"));
        dataSource.setUrl(env.getProperty("jdbc.url"));

        return dataSource;
    }

    @Autowired
    private YamlDbConfigProperties yamlDbConfigProperties;
    /*
    @Bean
    @Primary
    */
    public DataSource getDataSource2() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(yamlDbConfigProperties.getUser());
        dataSource.setPassword(yamlDbConfigProperties.getPassword());
        dataSource.setUrl(yamlDbConfigProperties.getUrl());
        dataSource.setDriverClassName(yamlDbConfigProperties.getDriver());

        return dataSource;
    }

    /*
    @Bean
    @Primary
    */
    public DataSource getDataSource3(@Value("${jdbc.user}") String user,
                                     @Value("${jdbc.password}") String password,
                                     @Value("${jdbc.driver}") String driver,
                                     @Value("${jdbc.url}") String url) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);

        return dataSource;
    }

}
